package com.infore.dataReceiving.quartz;
import java.util.Date;
import java.util.Map;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;

/**
 * 任务调度处理
 * @author zzh
 *
 */
@Configuration
public class QuartzScheduler {
    
    private static boolean flag = true;

    /**
     * 获取Job信息
     * 
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name, String group,Scheduler scheduler) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if(cronTrigger==null) {
        	return null;
        }else {
        	return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                    scheduler.getTriggerState(triggerKey).name());
        }
    }

    /**
     * 修改某个任务的执行时间
     * 
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String group, String time,Scheduler scheduler) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return date != null;
    }

    /**
     * 暂停所有任务
     * 
     * @throws SchedulerException
     */
    public void pauseAllJob(Scheduler scheduler) throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void pauseJob(String name, String group,Scheduler scheduler) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复所有任务
     * 
     * @throws SchedulerException
     */
    public void resumeAllJob(Scheduler scheduler) throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void resumeJob(String name, String group,Scheduler scheduler) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个任务
     * 
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group,Scheduler scheduler) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.deleteJob(jobKey);
    }

    public void addJob(Scheduler scheduler,Map<String, String> czMap) throws SchedulerException {
//    	if(czMap.isEmpty()) {
//    		czMap.put("11", "1");
//    		czMap.put("22", "1");
//    		czMap.put("33", "1");
//    	}
    	for(String key : czMap.keySet()) {
    		String msg = getJobInfo(key, "airGroup",scheduler);
    		if(msg==null) {
    			// 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
                // JobDetail 是具体Job实例
                JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob.class).withIdentity(key, "airGroup").build();
                // 基于表达式构建触发器
                CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/2 * * * ?");
                // CronTrigger表达式触发器 继承于Trigger
                // TriggerBuilder 用于构建触发器实例
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(key, "airGroup")
                        .withSchedule(cronScheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);
    		}
    	}
    }
    
    
    
    public static void main(String[] args) throws Exception {
    	SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        JobDetail job = JobBuilder.newJob(SchedulerQuartzJob.class)             //print "hello -- "+ new Date()
                .withIdentity("11", "group1")
                .build();
        CronScheduleBuilder cronScheduleBuilder1 = CronScheduleBuilder.cronSchedule("0 0/2 * * * ?");
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("11", "group1")
                .startNow()
                .withSchedule(cronScheduleBuilder1)
                .build();
        sched.scheduleJob(job, trigger);
        sched.start();
        System.out.println("=========================");
        JobDetail job2 = JobBuilder.newJob(SchedulerQuartzJob.class)          //print "world! --- "+new Date()
                .withIdentity("22", "group1")
                .build();
        CronScheduleBuilder cronScheduleBuilder2 = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        CronTrigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity("22", "group1")
                .startNow()
                .withSchedule(cronScheduleBuilder2)
                .build();
        sched.scheduleJob(job2,trigger2);
	}
   
}
